package com.zyk.leetcode;


/**
 * @author zhangsan
 * @date 2021/5/7 8:43
 */
public class C1486 {

    // 1. 暴力解还原数组
    public static int xorOperation(int n, int start) {
        int[] arr = new int[n];
        int ans = 0;
        for (int i = 0; i < n; i++) {
            arr[i] = start + (i << 1);
            ans ^= arr[i];
        }
        return ans;
    }

    // 空间压缩
    public static int xorOperation2(int n, int start) {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans ^= start + (i << 1);
        }
        return ans;
    }


    public static int xorOperation3(int n, int start) {
        int s = start >> 1, e = n & start & 1;
        int ret = sumXor(s - 1) ^ sumXor(s + n - 1);
        return ret << 1 | e;
    }

    public static int sumXor(int x) {
        if (x % 4 == 0) {
            return x;
        }
        if (x % 4 == 1) {
            return 1;
        }
        if (x % 4 == 2) {
            return x + 1;
        }
        return 0;
    }

    // for test
    public static void main(String[] args) {
        int n = 7,
                start = 3;
//        int n = 10,
//                start = 5;
        System.out.println(xorOperation(n, start));
        System.out.println(xorOperation2(n, start));
        System.out.println(xorOperation3(n, start));
    }

}
